package  com.diamond.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diamond.bo.FormulaBO;
import com.diamond.bo.ReturnBO;
import com.diamond.facade.FormulaFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.JsonUtils;
import com.diamond.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * PriceAction ������
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class FormulaAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1067256755983602452L;
	private HttpServletRequest request;
	private static Logger log=LoggerFactory.getLogger(FormulaAction.class);
	private FormulaFacade formulaFacade;
	private FormulaBO formulaBo;
	private List<FormulaBO> dataList;
	private String tips;
	private String json;
	
	/**
	 * 查询公式
	 * @return
	 */
	public String toformula() {
		Map session = ActionContext.getContext().getSession(); 
	    Object obj=session.get("USER_ID");
	    if(obj==null)return SUCCESS;
	    Long id=(Long) obj;
	    dataList= formulaFacade.findByUserID(id,null);
		return SUCCESS;
	}
	/**
	 * 保存公式
	 * @return
	 */
	public String saveFormula(){
//		System.out.println("**************saveFormula****************");
		tips="提交成功.";
		Map session = ActionContext.getContext().getSession(); 
		Integer code=formulaFacade.save(formulaBo,session);
		if(code!=0){
			tips="提交失败.";
			if(code==-2)tips="不允许设置重复重要等级的公式.";
		}
		return SUCCESS;
	}
	/**
	 * 修改公式
	 * @return
	 */
	public String editFormula(){
		json=JsonMSG.failed("-1").toJson();
		String id=ServletActionContext.getRequest().getParameter("formulaid");
		if(StringUtil.isEmpty(id))return SUCCESS;
		Object obj=formulaFacade.findById(new Long(id));
		try {
			formulaBo = new FormulaBO();
			PropertyUtils.copyProperties(formulaBo, obj);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		json=JsonMSG.sucess(formulaBo).toJson();
		return SUCCESS;
	}
	/**
	 * 刪除公式
	 * @return
	 */
	public String delFormula(){
		String id=ServletActionContext.getRequest().getParameter("formulaid");
		if(StringUtil.isEmpty(id))return SUCCESS;
		formulaFacade.deleteByKey(new Long(id));
		return SUCCESS;
	}
	//---------------------------------------------------------------------------

	/**
	 * 查询公式
	 */
	public String queryFormula() {
//		System.out.println("**************后台公式查询***************");
		Map session = ActionContext.getContext().getSession(); 
	    Object obj=session.get("USER_ID");
	    if(obj==null){
	    	this.json=JsonMSG.failed("查询失败，请您先登录！").toString();
	    } else {
	    	request = ServletActionContext.getRequest();
	    	request = ServletActionContext.getRequest();
			String start = request.getParameter("start");
			String pageSize = request.getParameter("limit");
			if (null == start || "".equals(start) || null == pageSize || "".equals(pageSize)) {
				start = "0";
				pageSize = "15";
			} 
	    	Long id=(Long) obj;
	    	dataList= formulaFacade.findByUserID(id,null);
	    	Integer totalSize = formulaFacade.countFormulaBo(id, Integer.valueOf(start), Integer.valueOf(pageSize));
	    	for (FormulaBO bo : dataList) {
	    		if (null != bo.getWeight() && !"".equals(bo.getWeight())) {
	    			bo.setWeightFrom(bo.getWeight().split("-")[0]);
	    			bo.setWeightTo(bo.getWeight().split("-")[1]);
	    		}
	    	}
//	    	this.json = JsonMSG.sucess(dataList).toJson();
	    	ReturnBO<FormulaBO> returnBo = new ReturnBO<FormulaBO>();
	    	returnBo.setPageSize(Integer.valueOf(pageSize));
	    	returnBo.setStart(Integer.valueOf(start));
	    	returnBo.setReturnBolist(dataList);
	    	returnBo.setTotalSize(totalSize);
	    	this.json=JsonMSG.sucess(returnBo).toJson();
	    }
		return "json";
	}
	
	/**
	 * 添加和修改公式
	 * @return
	 */
	public String addOrUpdateFormula() {
//		System.out.println("**************添加和修改公式****************");
		try {
			formulaBo=(FormulaBO) JsonUtils.toObject(json, FormulaBO.class);
			if (!"".equals(formulaBo.getWeightFrom()) && null != formulaBo.getWeightFrom()
					&& !"".equals(formulaBo.getWeightTo()) && null != formulaBo.getWeightTo()) {
				formulaBo.setWeight(formulaBo.getWeightFrom() + "-" + formulaBo.getWeightTo());
			}
			Map session = ActionContext.getContext().getSession(); 
			Integer code=formulaFacade.save(formulaBo,session);
			if (code == 0) {
				this.json = JsonMSG.sucess("恭喜，操作成功").toJson();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("对不起，操作出错",e);
			this.json=JsonMSG.failed("操作出错!详细:"+e).toJson();
		}
			return "json";
	}
	
	/**
     * 标题公式名
     */
    public String checkFormulaName(){
	
    	return null;
    }
    
    /**
	 * 根据FormulaId查询公式详细信息
	 * @return
	 */
	public String findFormulaByFormulaId() {
//		System.out.println("*******根据formulaId查询公式详细信息*********");
		try {
			String formulaId=json;
			FormulaBO returnFormulaBo = formulaFacade.findMessageByMessageId(formulaId);
			if (null != returnFormulaBo.getWeight() && !"".equals(returnFormulaBo.getWeight())) {
				returnFormulaBo.setWeightFrom(returnFormulaBo.getWeight().split("-")[0]);
				returnFormulaBo.setWeightTo(returnFormulaBo.getWeight().split("-")[1]);
    		}
			this.json=JsonMSG.sucess(returnFormulaBo).toJson();
		} catch (Exception e) {
			log.error("查询公式明细出错 ",e);
			this.json=JsonMSG.failed("系统未找到该 公式:"+json).toJson();
		}
		return "json";
	}
    
	/**
     * 删除公式
     * @return
     */
	public String deleteFormula() {
//		System.out.println("***************删除公式******************");
		try {
			String[] formulaIds = json.split(";");
			if (formulaIds.length > 0) {
				for (String formulaId : formulaIds) {
					if (StringUtil.isEmpty(formulaId))
						return SUCCESS;
					formulaFacade.deleteByKey(Long.valueOf(formulaId));
					this.json = JsonMSG.sucess("公式已删除!").toJson();
				}
			} else {
				this.json = JsonMSG.failed("请选择需要删除的公式！").toJson();
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.json = JsonMSG.failed("删除公式出错!详细:" + e).toJson();
			log.error("删除公式出错!详细:", e);
		}
		return "json";
	}
	
/***************************************************************************/	
	
	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}


	public void setFormulaFacade(FormulaFacade formulaFacade) {
		this.formulaFacade = formulaFacade;
	}

	public FormulaBO getFormula() {
		return formulaBo;
	}

	public void setFormula(FormulaBO formula) {
		this.formulaBo = formula;
	}
	public String getTips() {
		return tips;
	}
	public void setTips(String tips) {
		this.tips = tips;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
}
